import torch 
import torch.nn as nn 

class Perceptron(nn.Module):
    def __init__(self):
        super().__init__() 
        self.fc = nn.Linear(1,1)

    def forward(self, x):      
        y = self.fc.forward(x)
        return y

perceptron_path = 'perceptron_model.pth'  # 定义保存模型参数的文件路径
loaded_perceptron = torch.load(perceptron_path)  # 使用torch.load函数从文件中载入模型参数
loaded_model_perceptron = Perceptron()  # 实例化一个新的感知机模型，用于加载模型参数
loaded_model_perceptron.load_state_dict(loaded_perceptron)  # 使用load_state_dict函数将载入的参数加载到模型中
loaded_model_perceptron.eval()  # 将模型设置为评估模式，关闭梯度计算和批归一化等操作
print('perceptron model structure:')
print(loaded_model_perceptron)
with torch.no_grad():
    x_test = torch.tensor([10.0])
    y_test = loaded_model_perceptron(x_test.unsqueeze(0))
    print(f'Text input: {x_test.item()}',f'Text output: {y_test.item()}')  
    
class Perceptron2(nn.Module):
    def __init__(self):
        super(Perceptron2, self).__init__()
        self.fc1 = nn.Linear(1,2)
        self.fc2 = nn.Linear(2,1)       
    def forward(self, x):      
        y = torch.relu(self.fc1(x))
        y = self.fc2(y)  
        return y
net_path = 'net_model.pth'  # 定义保存模型参数的文件路径
loaded_net = torch.load(net_path)  # 使用torch.load函数从文件中载入模型参数
loaded_model_net = Perceptron2()  # 实例化一个新的感知机模型，用于加载模型参数
loaded_model_net.load_state_dict(loaded_net)  # 使用load_state_dict函数将载入的参数加载到模型中
loaded_model_net.eval()  # 将模型设置为评估模式，关闭梯度计算和批归一化等操作
print('net model structure:')
print(loaded_model_net)
with torch.no_grad():
    x_test = torch.tensor([10.0])
    y_test = loaded_model_net(x_test.unsqueeze(0))
    print(f'Text input: {x_test.item()}',f'Text output: {y_test.item()}')  